<?php defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * Base class for controller in admin page. Basic task is checking user login.
 */
class MY_Controller extends CI_Controller {
    protected $data;

    function __construct() {
        parent::__construct();
        $this->load->library('Ion_auth');
	$this->load->library('session');
        $this->load->library('form_validation');

        // set language
        $this->lang->load('form_validation','indonesia');
        $this->lang->load('Ion_auth','indonesia');

        // Check authentication
        if (!$this->ion_auth->logged_in()) {
            $uri = $this->uri->uri_string();
            $this->session->set_userdata('uri', $uri);

            redirect('auth', 'refresh');
        }

        if($this->ion_auth->is_admin()){
            $this->session->set_userdata('username','Admin');
            $this->session->set_userdata('role','admin');
        }
        else{
            $user = $this->ion_auth->get_user();
            $this->session->set_userdata('username',$user->first_name.' '.$user->last_name);
            $this->session->set_userdata('role','user');
        }
        
        $this->data['userdata'] = $this->session->userdata;

        if($this->session->userdata('uri')!=null){
            $uri = $this->session->userdata('uri');
            $this->session->unset_userdata('uri');

            redirect($uri,'refresh');
        }
        
        $this->data['message'] = $this->session->flashdata('message');
    }

}

?>
